﻿/*
 * OpenDoPE Word Add-In
 * Copyright (C) 2010-2011  Plutext Pty Ltd
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * 
 * For details, see http://www.gnu.org/licenses/gpl-2.0.html
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using NLog;

using Word = Microsoft.Office.Interop.Word;
using Office = Microsoft.Office.Core;


namespace OpenDoPE_Wed
{
    public partial class UserControlConditionButtons : UserControl
    {

        static Logger log = LogManager.GetLogger("OpenDoPE_Wed");

        WedTaskPane taskPane;

        public UserControlConditionButtons(WedTaskPane taskPane)
            //Word.ContentControl currentCC, XmlEditorControl xec)
        {
            this.taskPane = taskPane;

            //this.currentCC = currentCC;
            //this.xec = xec;

            InitializeComponent();
        }

        public void initFields()
        {
            Word.ContentControl currentCC = taskPane.currentCC;
            TagData td = new TagData(currentCC.Tag);
            if (td.get("od:condition") != null)
            {
                this.textBoxConditionID.Text = td.get("od:condition");
            }
        }

        private void buttonApply_Click(object sender, EventArgs e)
        {
            log.Debug("condition clicked..");

            Word.ContentControl currentCC = taskPane.currentCC;
            XmlEditorControl xec = taskPane.xmlEditor;

            if (!xec.isWellFormed()) return;

            // Save the conditions part XML
            xec.commitChanges();

            if (string.IsNullOrWhiteSpace(this.textBoxConditionID.Text))
            {
                Mbox.ShowSimpleMsgBoxError("You need to enter a valid condition id");
                return;
            }
            try
            {
                System.Xml.XmlConvert.VerifyNCName(this.textBoxConditionID.Text.Trim());

            }
            catch (System.Xml.XmlException)
            {
                Mbox.ShowSimpleMsgBoxError("Please use an NCName for the Condition ID (hint: no punctuation) ");
                return;
            }

            // TODO check the id entered actually exists

            // Now write it to the tag 

            if (currentCC == null)
            {
                log.Error("currentCC not set?!");
                return;
            }

            // Write tag
            TagData td = new TagData(currentCC.Tag);
            td.remove("od:repeat");
            td.remove("od:condition");
            td.set("od:condition", this.textBoxConditionID.Text.Trim() );
            currentCC.Tag = td.asQueryString();


            log.Debug("Condition written!");
            Mbox.ShowSimpleMsgBoxInfo("Condition written!");

        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {

            Word.ContentControl currentCC = taskPane.currentCC;

            if (currentCC == null)
            {
                log.Error("currentCC not set?!");
                return;
            }

            // Write tag
            TagData td = new TagData(currentCC.Tag);

            try
            {
                if (td.get("od:condition") == null)
                {
                    Mbox.ShowSimpleMsgBoxInfo("This content control isn't a condition.");
                    return;
                }
            }
            catch (Exception ex)
            {
            }

            td.remove("od:condition");
            currentCC.Tag = td.asQueryString();

            Mbox.ShowSimpleMsgBoxInfo("Condition removed from this content control");

        }

        private void buttonXPaths_Click(object sender, EventArgs e)
        {
            FormXPathPartViewer form = new FormXPathPartViewer(taskPane);
            form.Show();
        }
    }
}
